<template>
  <div class="dashboard-editor-container">
    <github-corner class="github-corner" />

    <panel-group @handleSetLineChartData="handleSetLineChartData" />

    <!-- <el-row style="background: #fff; padding: 16px 16px 0; margin-bottom: 32px">
      <line-chart :chart-data="lineChartData" />
    </el-row> -->
    <el-row style="background: #fff; padding: 16px 16px 0; margin-bottom: 32px">
      <bar-chart-data
        :xAxisData="xAxisTotalData"
        :seriesData="seriesTotalData"
      />
    </el-row>

    <el-row :gutter="24">
      <el-col
        :xs="24"
        :sm="24"
        :lg="12"
        v-if="
          this.$store.getters.roles.toString() ===
          '772a7b13dabc015c495d9056aed54a85'
        "
      >
        <div class="chart-wrapper">
          <bar-chart-data
            :xAxisData="xAxisUserData"
            :seriesData="seriesUserData"
          />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          <bar-chart-data
            :xAxisData="xAxisProductData"
            :seriesData="seriesProductData"
          />
        </div>
      </el-col>
    </el-row>

    <el-row
      :gutter="32"
      v-if="
        this.$store.getters.roles.toString() ===
        '772a7b13dabc015c495d9056aed54a85'
      "
    >
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart-data
            :legendData="legendOrganizeData"
            :seriesData="seriesOrganizeData"
            :title="`Organization Manager`"
          />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart-data
            :legendData="legendWeightData"
            :seriesData="seriesWeightData"
            :title="`Weight Announcement`"
          />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart-data
            :legendData="legendTypeData"
            :seriesData="seriesTypeData"
            :title="`Type Announcement`"
          />
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <raddar-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <bar-chart />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import GithubCorner from "@/components/GithubCorner";
import PanelGroup from "./components/PanelGroup";
import LineChart from "./components/LineChart";
import RaddarChart from "./components/RaddarChart";
import PieChart from "./components/PieChart";
import BarChart from "./components/BarChart";
import BarChartData from "./components/BarChartData";
import PieChartData from "./components/PieChartData";
import { getBarChartData, getTotalCount, getPieChartData } from "@/api/common";
const lineChartData = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145],
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130],
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130],
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130],
  },
};

export default {
  name: "DashboardAdmin",
  components: {
    GithubCorner,
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,
    BarChartData,
    PieChartData,
  },
  data() {
    return {
      lineChartData: lineChartData.newVisitis,
      xAxisUserData: [],
      xAxisProductData: [],
      xAxisTotalData: [],
      seriesUserData: [],
      seriesProductData: [],
      seriesTotalData: [],
      legendOrganizeData: [],
      legendWeightData: [],
      legendTypeData: [],
      seriesOrganizeData: [],
      seriesWeightData: [],
      seriesTypeData: [],
    };
  },
  created() {
    this.getData();
    this.getTotalData();
    this.getPieData();
  },
  methods: {
    handleSetLineChartData(type) {
      this.lineChartData = lineChartData[type];
    },
    getData() {
      getBarChartData().then((res) => {
        if (res.code === 200) {
          let userData = res.data.user;
          let productData = res.data.product;

          this.xAxisUserData = userData.map((user) => {
            return user.roleName;
          });
          this.seriesUserData = userData.map((user) => {
            return user.userCount;
          });

          this.xAxisProductData = productData.map((product) => {
            return product.productType;
          });

          this.seriesProductData = productData.map((product) => {
            return product.productGroupCount;
          });
        }
      });
    },
    getTotalData() {
      getTotalCount().then((res) => {
        if (res.code === 200) {
          this.xAxisTotalData = [
            "系统用户",
            "人员组织",
            "采购产品",
            "产品类型",
            "采购项目",
            "采购标案",
            "主业公告",
          ];
          let total = res.data;
          this.seriesTotalData = [
            total.accountCount,
            total.organizationCount,
            total.productCount,
            total.typeCount,
            total.projCount,
            total.tenderCount,
            total.announcementCount,
          ];
        }
      });
    },
    getPieData() {
      getPieChartData().then((res) => {
        if (res.code === 200) {
          this.legendOrganizeData = res.data.organizeCount.map((o) => {
            return o.organizeName;
          });
          res.data.organizeCount.forEach((o) => {
            const data = {
              value: o.userCount,
              name: o.organizeName,
            };
            this.seriesOrganizeData.push(data);
          });

          this.legendTypeData = res.data.typeAnnounce.map((t) => {
            return t.type === 1 ? "系统公告" : "标案公告";
          });
          res.data.typeAnnounce.forEach((t) => {
            const data = {
              value: t.announceCount,
              name: t.type === 1 ? "系统公告" : "标案公告",
            };
            this.seriesTypeData.push(data);
          });

          this.legendWeightData = res.data.weightAnnounce.map((w) => {
            return w.weight === 0 ? "非置顶公告" : "置顶公告";
          });
          res.data.weightAnnounce.forEach((w) => {
            const data = {
              value: w.announceCount,
              name: w.weight === 0 ? "非置顶公告" : "置顶公告",
            };
            this.seriesWeightData.push(data);
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .github-corner {
    position: absolute;
    top: 0px;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
